#!/bin/bash

# 定义颜色代码
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# 默认参数
INTERVAL=5
DURATION=60
LOG_DIR="storage_logs_$(date +%Y%m%d_%H%M%S)"
PERF_OUTPUT="storage_performance_report_$(date +%Y%m%d_%H%M%S).txt"

# 解析命令行参数
while [[ $# -gt 0 ]]; do
    case $1 in
        -i|--interval)
            INTERVAL="$2"
            shift 2
            ;;
        -d|--duration)
            DURATION="$2"
            shift 2
            ;;
        -o|--output)
            PERF_OUTPUT="$2"
            shift 2
            ;;
        *)
            echo "未知选项: $1"
            exit 1
            ;;
    esac
done

# 验证执行权限
if [[ $EUID -ne 0 ]]; then
   echo -e "${YELLOW}[警告] 必须使用root用户执行本脚本${NC}" 
   exit 1
fi

# 创建日志目录
mkdir -p "$LOG_DIR"

# 定义执行函数
function run_step() {
    if [ "$1" = "period_monitor.sh" ]; then
        ./subscripts/$1 -i "$INTERVAL" -d "$DURATION" -o "$LOG_DIR/$PERF_OUTPUT"
    else
        ./subscripts/$1
        # 移动生成的日志文件到日志目录
        if [ "$1" = "disk_static_info.sh" ]; then
            mv disk_static_info_*.txt "$LOG_DIR/" 2>/dev/null
        elif [ "$1" = "filesystem_static_info.sh" ]; then
            mv filesystem_static_info_*.txt "$LOG_DIR/" 2>/dev/null
        fi
    fi
    echo -e "${GREEN}✓ $2${NC}"
}

# 按顺序执行各步骤
run_step "disk_static_info.sh" "磁盘静态信息收集完成"
run_step "filesystem_static_info.sh" "文件系统静态信息收集完成"
run_step "period_monitor.sh" "存储性能监控完成"

echo -e "\n${GREEN}所有监控任务已完成"
echo -e "报告文件已保存在目录: $LOG_DIR${NC}"

